﻿using PacoToolKit.Format;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Protocol.Flowmeter
{
    public static class Decoder
    {
        /// <summary>
        /// 解析Modbus
        /// </summary>
        /// <param name="str">中继帧报文</param>
        /// <param name="di">数据项</param>
        /// <param name="fmeterType">流量计类型</param>
        /// <returns></returns>
        public static FloatBlock DecodeModbus(string str, string di, int fmeterType)
        {
            var block = new FloatBlock();
            block.Addr = str.Substring(0, 2);//10号为0A
            block.FunCode = str.Substring(2, 2);
            block.DataLength = int.Parse(Formater.HEXtoDEC(str.Substring(4, 2)));


            if (block.DataLength == 2)
            {
                if (di == "0000" && fmeterType == 8)//压力变送器
                {
                    int p = int.Parse(Formater.HEXtoDEC(str.Substring(6, 2 * 2)));
                    block.DataValue = decimal.Parse(((double)p / 2000 * 1).ToString());
                }
            }
            else if (block.DataLength == 4)
            {
                if (di == "1012") //水流量计 正向总量
                {
                    long a = long.Parse(Formater.HEXtoDEC(str.Substring(6, 4 * 2)));
                    block.DataValue = a;
                }
                else if (fmeterType == 9) //冷热量
                {
                    if (di == "0026" || di == "0028" || di == "0008" || di == "0022") //温度、流速、Q值
                    {
                        var hex = Formater.ReverseHexStringHighLow(str.Substring(6, block.DataLength * 2));
                        string temp = Formater.HexStringToFloat(hex).ToString("F");
                        block.DataValue = decimal.Parse(temp);
                    }
                    else if (di == "0019" || di == "0016" || di == "0013") //累计冷量 累计热量  累计能量
                    {
                        var hex = Formater.ReverseHexStringHighLow(str.Substring(6, block.DataLength * 2));
                        block.DataValue = decimal.Parse(Formater.FourByteHEXtoDEC(hex));
                    }
                }
                else if (di == "0000")
                {
                    if (fmeterType == 6)
                    {
                        //温湿度变送器(485型)
                        string v = str.Substring(6, 4 * 2);
                        var val1 = v.Substring(0, 4);
                        var val2 = v.Substring(4, 4);
                        List<SpecialDataValue> list = new List<SpecialDataValue>();
                        SpecialDataValue sd = new SpecialDataValue();
                        //温度计算
                        sd.Value = decimal.Parse((decimal.Parse(Formater.TwoByteHEXtoDEC(val2)) / 10).ToString());
                        sd.DI = "0000T";
                        list.Add(sd);
                        //湿度计算
                        sd = new SpecialDataValue();
                        sd.Value = decimal.Parse((decimal.Parse(Formater.TwoByteHEXtoDEC(val1)) / 10).ToString());
                        sd.DI = "0000H";
                        list.Add(sd);
                        block.SpecialDataValueList = list;
                    }
                    else
                    {
                        string temp = Formater.HexStringToFloat(str.Substring(6, block.DataLength * 2)).ToString("F");
                        block.DataValue = decimal.Parse(temp);
                    }
                }
                else
                {
                    //float data
                    var hex = str.Substring(6, block.DataLength * 2);
                    string temp = Formater.HexStringToFloat(hex).ToString("F");
                    block.DataValue = decimal.Parse(temp);
                }
            }
            else if (block.DataLength == 6)
            {
                if (fmeterType == 9) //冷热量
                {
                    if (di == "0019" || di == "0016" || di == "0013") ////累计冷量 累计热量  累计能量
                    {
                        var hex = Formater.ReverseHexStringHighLow(str.Substring(6, 4 * 2));//前四个字节代表内容，后1个字节代表指数

                        var zhishu_hex = str.Substring(14, 2 * 2);//前四个字节代表内容，后2个字节代表指数
                        //指数代表的是0的个数
                        int zhishu_num = int.Parse(Formater.HEXtoDEC(zhishu_hex));

                        var real = int.Parse(Formater.FourByteHEXtoDEC(hex)) * (Math.Pow(10, zhishu_num));

                        block.DataValue = decimal.Parse(real.ToString());
                    }
                }
            }
            else if (block.DataLength == 8)
            {
                if (di == "0314")
                {
                    //燃气流量计 标况累积
                    string bcd = str.Substring(6, 8 * 2);
                    decimal value = decimal.Parse(bcd.Insert(10, "."));
                    block.DataValue = value;
                }
                else if (di == "0001")
                {
                    ////天信流量计读总量
                    var val1 = str.Substring(6, 4 * 2);
                    var val2 = str.Substring(14, 4 * 2);
                    float str1 = Formater.HexStringToFloat(val1);
                    float str2 = Formater.HexStringToFloat(val2);
                    block.DataValue = decimal.Parse(str1.ToString().PadRight(7, '0')) + decimal.Parse(str2.ToString("F"));
                }

                else
                {
                    //long data + float data
                    long a = long.Parse(Formater.HEXtoDEC(str.Substring(6, 4 * 2)));
                    decimal b = decimal.Parse(Formater.HexStringToFloat(str.Substring(14, 4 * 2)).ToString());
                    block.DataValue = (a + b);
                }
            }
            else if (block.DataLength == 24)
            {
                //天信流量计读全部(完全读)
                List<SpecialDataValue> list = new List<SpecialDataValue>();

                //总量
                SpecialDataValue sd = new SpecialDataValue();
                string val1 = str.Substring(6, 4 * 2);
                string val2 = str.Substring(14, 4 * 2);
                float str1 = Formater.HexStringToFloat(val1);
                float str2 = Formater.HexStringToFloat(val2);
                sd.Value = decimal.Parse(str1.ToString().PadRight(7, '0')) + decimal.Parse(str2.ToString("F"));
                sd.DI = "0001";
                list.Add(sd);

                //标况
                sd = new SpecialDataValue();
                string val = str.Substring(22, 4 * 2);
                string temp = Formater.HexStringToFloat(val).ToString("F");
                sd.Value = decimal.Parse(temp);
                sd.DI = "0005";
                list.Add(sd);

                //工况 暂时忽略

                //温度
                sd = new SpecialDataValue();
                val = str.Substring(38, 4 * 2);
                temp = Formater.HexStringToFloat(val).ToString("F");
                sd.Value = decimal.Parse(temp);
                sd.DI = "0009";
                list.Add(sd);

                //压力
                sd = new SpecialDataValue();
                val = str.Substring(46, 4 * 2);
                temp = Formater.HexStringToFloat(val).ToString("F");
                sd.Value = decimal.Parse(temp);
                sd.DI = "000B";
                list.Add(sd);

                block.SpecialDataValueList = list;
            }

            return block;
        }

        public static FloatBlock DecodeInfraredTemSensorMsg(string str)
        {
            var block = new FloatBlock();
            block.Addr = str.Substring(4, 2);//10号为0A
            string dataH = str.Substring(8, 2);
            string dataL = str.Substring(10, 2);
            decimal value = (decimal.Parse(Formater.HEXtoDEC(dataH)) * 256 + decimal.Parse(Formater.HEXtoDEC(dataL))) / 10;
            block.DataValue = value;
            return block;
        }
    }
}
